﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JYStockAnalyser.Face;
using JYStockAnalyser.Model;

namespace JYStockAnalyser.ClientLib
{
    /// <summary>
    /// 客户端缓存
    /// </summary>
    /// <remarks>一些频繁使用且不经常改动的数据，都通过此对象的单例获取，第一次使用这些数据时从服务器端获取然后缓存到内存中，以后的获取将直接读取客户端内存中的数据</remarks>
    public class ClientCache
    {
        static ClientCache m_instance;

        public static ClientCache Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new ClientCache();
                }
                return m_instance;
            }
        }

        YearQuarter[] m_minAndMaxYear;
        public YearQuarter[] MinAndMaxQuarter
        {
            get
            {
                if (m_minAndMaxYear == null)
                {
                    m_minAndMaxYear = new StockManageFace().GetMinAndQuarter();
                }
                return m_minAndMaxYear;
            }
        }

        public List<YearQuarter> GetAllYearQuarters()
        {
            List<YearQuarter> quarters = new List<YearQuarter>();
            for (int year = MinAndMaxQuarter[0].Year; year <= MinAndMaxQuarter[1].Year; year++)
            {
                for (int quarter = 1; quarter <= 4; quarter++)
                {
                    if (year == MinAndMaxQuarter[0].Year && quarter < MinAndMaxQuarter[0].Quarter) { continue; }

                    if (year == MinAndMaxQuarter[1].Year && quarter > MinAndMaxQuarter[1].Quarter) { break; }

                    quarters.Add(new YearQuarter(year, quarter));
                }
            }
            return quarters;
        }

        List<string> m_allStockNOAndNames;
        public List<string> AllStockNOAndNames
        {
            get
            {
                if (m_allStockNOAndNames == null)
                {
                    m_allStockNOAndNames = new StockManageFace().GetAllStockNOAndNames();
                }
                return m_allStockNOAndNames;
            }
        }

        int[][] m_defaultSingleStockAnalyseGuideLines;
        public int[][] DefaultSingleStockAnalyseGuideLines
        {
            get
            {
                if (m_defaultSingleStockAnalyseGuideLines == null)
                {
                    m_defaultSingleStockAnalyseGuideLines = new StockManageFace().GetDefaultSingleStockAnalyseGuideLines();
                }
                return m_defaultSingleStockAnalyseGuideLines;
            }
        }

        int[][] m_defaultFinancialAnalyseGuideLines;
        public int[][] DefaultFinancialAnalyseGuideLines
        {
            get
            {
                if (m_defaultFinancialAnalyseGuideLines == null)
                {
                    m_defaultFinancialAnalyseGuideLines = new StockManageFace().GetDefaultFinancialAnalyseGuideLines();
                }
                return m_defaultFinancialAnalyseGuideLines;
            }
        }

        List<string> m_trades;

        public List<string> AllTrades
        {
            get
            {
                if (m_trades == null)
                {
                    m_trades = new StockManageFace().GetAllTrades();
                }
                return m_trades;
            }
        }

        List<string> m_reports;
        /// <summary>
        /// 获取所有财务报表
        /// </summary>
        public List<string> AllReports
        {
            get
            {
                if (m_reports == null)
                {
                    m_reports = new StockManageFace().GetAllReports();
                }
                return m_reports;
            }
        }

        List<string> m_financialCategories;

        public List<string> FinancialCategories
        {
            get
            {
                if (m_financialCategories == null)
                {
                    m_financialCategories = new StockManageFace().GetAllFinancialGuideLineCategories();
                }
                return m_financialCategories;
            }
        }

        Dictionary<string, Dictionary<int, List<GuideLineType>>> m_allGuideLineTypes;

        /// <summary>
        /// 获取所有的指标类型
        /// 按财报->股票类型　两级分类
        /// </summary>
        public Dictionary<string, Dictionary<int, List<GuideLineType>>> AllGuideLineTypes
        {
            get
            {
                if (m_allGuideLineTypes == null)
                {
                    m_allGuideLineTypes = new StockManageFace().FindAllGuideLineTypes();
                }
                return m_allGuideLineTypes;
            }
        }

        Dictionary<string, Dictionary<int, List<GuideLineType>>> m_allFinancialGuideLineTypes;

        /// <summary>
        /// 获取所有的指标类型
        /// 按财报->股票类型　两级分类
        /// </summary>
        public Dictionary<string, Dictionary<int, List<GuideLineType>>> AllFinancialGuideLineTypes
        {
            get
            {
                if (m_allFinancialGuideLineTypes == null)
                {
                    m_allFinancialGuideLineTypes = new StockManageFace().FindAllFinancialGuideLineTypes();
                }
                return m_allFinancialGuideLineTypes;
            }
        }

        Dictionary<string, Dictionary<int, List<GuideLineType>>> m_allTypes;

        /// <summary>
        /// 获取所有指标，包括财务等
        /// 按报表->股票类型　两级分类
        /// </summary>
        public Dictionary<string, Dictionary<int, List<GuideLineType>>> AllTypes
        {
            get
            {
                if (m_allTypes == null)
                {
                    m_allTypes = new Dictionary<string, Dictionary<int, List<GuideLineType>>>();
                    foreach (KeyValuePair<string, Dictionary<int, List<GuideLineType>>> item in AllGuideLineTypes)
                    {
                        m_allTypes.Add(item.Key, item.Value);
                    }
                    foreach (KeyValuePair<string, Dictionary<int, List<GuideLineType>>> item in AllFinancialGuideLineTypes)
                    {
                        m_allTypes.Add(item.Key, item.Value);
                    }
                }
                return m_allTypes;
            }
        }

        /// <summary>
        /// 通过编号获取指标
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public GuideLineType GetGuideLineType(int no)
        {
            foreach (KeyValuePair<string, Dictionary<int, List<GuideLineType>>> item in AllGuideLineTypes)
            {
                foreach (KeyValuePair<int, List<GuideLineType>> item2 in item.Value)
                {
                    foreach (GuideLineType type in item2.Value)
                    {
                        if (type.NO.Equals(no))
                            return type;
                    }
                }
            }

            foreach (KeyValuePair<string, Dictionary<int, List<GuideLineType>>> item in AllFinancialGuideLineTypes)
            {
                foreach (KeyValuePair<int, List<GuideLineType>> item2 in item.Value)
                {
                    foreach (GuideLineType type in item2.Value)
                    {
                        if (type.NO.Equals(no))
                            return type;
                    }
                }
            }
            return null;
        }

        List<Stock> m_allStocks;
        public List<Stock> AllStocks
        {
            get
            {
                if (m_allStocks == null)
                {
                    m_allStocks = new StockManageFace().FindAllStocks();
                }
                return m_allStocks;
            }
        }

        /// <summary>
        /// 获取指定行业的所有股票
        /// </summary>
        /// <param name="trade"></param>
        /// <returns></returns>
        public List<Stock> GetStocksByTrade(string trade)
        {
            return AllStocks.FindAll(new Predicate<Stock>(delegate(Stock stock)
            {
                return stock.Trade.Equals(trade);
            }));
        }
    }
}
